package com.me.modules.questionnaire.controller;

import java.util.*;

import com.me.modules.common.controller.BaseController;
import com.me.modules.common.page.Page;
import com.me.modules.common.utils.*;
import com.me.modules.knowledge.entity.KnowledgeInfoEntity;
import com.me.modules.questionnaire.entity.*;
import com.me.modules.questionnaire.service.*;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.util.WebUtils;


/**
 * 调查问卷
 *
 * @author me
 * @email me@163.com
 * @date 2019-01-11 14:40:41
 */
@Controller
@RequestMapping("/questionnaire")
public class QuestionnaireInfoController extends BaseController{
	@Autowired
	private QuestionnaireInfoService questionnaireInfoService;
	@Autowired
	private QuQuestionService quQuestionService;
	@Autowired
	private QuCheckboxService checkboxService;
	@Autowired
	private QuRadioService radioService;
	@Autowired
	private QuSurveyAnswerService surveyAnswerService;
	@Autowired
	private QuestionnaireUserService questionnaireUserService;

	/**
	 * 问卷列表
	 */
	@RequestMapping("/list")
//	@RequiresPermissions("questionnaireinfo:list")
	public String list(QuestionnaireInfoEntity questionnaireInfoEntity, Model model, HttpServletRequest request){
		//查询列表数据
		int pageNum = Utils.parseInt(request.getParameter("pageNum"), 1);
		Page<KnowledgeInfoEntity> page = questionnaireInfoService.findPage(questionnaireInfoEntity, pageNum);
		model.addAttribute("page", page);
		return "questionnaire/questionnaireList";
	}
	/**
	 * 答卷列表
	 */
	@RequestMapping("/answerList")
//	@RequiresPermissions("questionnaireinfo:list")
	public String answerList(QuestionnaireInfoEntity questionnaireInfoEntity, Model model, HttpServletRequest request){
		//查询列表数据
		int pageNum = Utils.parseInt(request.getParameter("pageNum"), 1);
		Page<KnowledgeInfoEntity> page = questionnaireInfoService.findanswerPage(questionnaireInfoEntity, pageNum,getUserId());
		model.addAttribute("page", page);
		return "questionnaire/answerList";
	}
	/**
	 *保存问卷答案
	 */
	@RequestMapping("/saveAnswer")
//	@RequiresPermissions("questionnaireinfo:delete")
	public String saveAnswer(Model model, String id, HttpServletRequest request){
		QuSurveyAnswerEntity surveyAnswerEntity = new QuSurveyAnswerEntity();
		surveyAnswerEntity.setUserId(getUserId());
		surveyAnswerEntity.setSurveyId(Integer.parseInt(id));
		Map<String, Map<String, Object>> quMaps = buildSaveSurveyMap(request);
		surveyAnswerService.saveAnswer(surveyAnswerEntity, quMaps);
		return "questionnaire/responseSuccess";
	}

	/**
	 * 查看统计结果
	 */
	@RequestMapping("/defaultReport")
	public String defaultReport(Model model, String id, HttpServletRequest request) {
		QuestionnaireInfoEntity questionnaireInfo = questionnaireInfoService.queryObject(id);
		int total = surveyAnswerService.queryAnswerTotal(id);
		List<QuQuestionEntity> questionEntityList = quQuestionService.getQuestions(questionnaireInfo.getId());
		for (QuQuestionEntity questionEntity : questionEntityList) {
			List<QuCheckboxEntity> checkboxs = checkboxService.getCheckboxsAnswer(questionEntity.getId());
			questionEntity.setQuCheckboxs(checkboxs);
			List<QuRadioEntity> radios = radioService.getRadiosAnswer(questionEntity.getId());
			questionEntity.setQuRadios(radios);
			questionEntity.setAnCount(total);
		}
		questionnaireInfo.setQuestions(questionEntityList);
		model.addAttribute("survey", questionnaireInfo);
		model.addAttribute("surveyStyle", null);
		return "questionnaire/defaultReport";
	}
	/**
	 * 获取统计数据 //暂时不用
	 */
	@RequestMapping("/chartData")
	@ResponseBody
	public Result chartData(Model model, String quId, HttpServletRequest request) {
		List list = new ArrayList();
		Map map = new HashMap();
		map.put("count" , 0);
		map.put("optionName", "haha");
		map.put("colDataCrosss", null);

		list.add(map);
		Map link = new LinkedHashMap();
		link.put("0",map);
		map = new HashMap();
		map.put("count" , 8);
		map.put("optionName", "haha");
		map.put("colDataCrosss", null);
		link.put("1",map);
		JSONArray jsonArray = JSONArray.fromObject(list);

		return Result.ok(link);
	}
	/**
	 * 生成问卷
	 */
	@RequestMapping("/answerSurvey")
	public String answerSurvey(Model model, String id, HttpServletRequest request) {
		QuestionnaireInfoEntity questionnaireInfo = questionnaireInfoService.queryObject(id);
		List<QuQuestionEntity> questionEntityList = quQuestionService.getQuestions(questionnaireInfo.getId());
		for (QuQuestionEntity questionEntity : questionEntityList) {
			List<QuCheckboxEntity> checkboxs = checkboxService.getCheckboxs(questionEntity.getId());
			questionEntity.setQuCheckboxs(checkboxs);
			List<QuRadioEntity> radios = radioService.getRadios(questionEntity.getId());
			questionEntity.setQuRadios(radios);
		}
		questionnaireInfo.setQuestions(questionEntityList);
		model.addAttribute("survey", questionnaireInfo);
		model.addAttribute("surveyStyle", null);


		return "questionnaire/answerSurvey";
	}
	/**
	 * 发布问卷
	 */
	@RequestMapping("/publishSurvey")
	@ResponseBody
	public Result publishSurvey(Model model, String id, HttpServletRequest request) {
		QuestionnaireInfoEntity questionnaireInfo = questionnaireInfoService.queryObject(id);
		questionnaireInfo.setStatus("2");
		questionnaireInfoService.update(questionnaireInfo);
		return Result.ok();
	}

	/**
	 * 设计问卷
	 */
	@RequestMapping("/designSurvey")
	public String designSurvey(Model model, String id, HttpServletRequest request) {
		QuestionnaireInfoEntity questionnaireInfo = questionnaireInfoService.queryObject(id);
		List<QuQuestionEntity> questionEntityList = quQuestionService.getQuestions(questionnaireInfo.getId());
		for (QuQuestionEntity questionEntity : questionEntityList) {
			List<QuCheckboxEntity> checkboxs = checkboxService.getCheckboxs(questionEntity.getId());
			questionEntity.setQuCheckboxs(checkboxs);
			List<QuRadioEntity> radios = radioService.getRadios(questionEntity.getId());
			questionEntity.setQuRadios(radios);
		}
		questionnaireInfo.setQuestions(questionEntityList);
		model.addAttribute("survey", questionnaireInfo);
		model.addAttribute("surveyStyle", null);


		return "questionnaire/designSurvey";
	}
	/**
	 * 新增问卷
	 */
	@RequestMapping("/addSurvey")
	@ResponseBody
	public Result addSurvey(Model model, String title, HttpServletRequest request) {
		QuestionnaireInfoEntity questionnaireInfo = new QuestionnaireInfoEntity();
		questionnaireInfo.setName(title);
		questionnaireInfo.setStatus("1");
		questionnaireInfoService.save(questionnaireInfo);
		return Result.ok().put("questionnaireInfo", questionnaireInfo);
	}
	/**
	 * 修改问卷标题
	 */
	@RequestMapping("/updateSurvey")
	@ResponseBody
	public Result updateSurvey(Model model, String id, HttpServletRequest request,String surveyId,String svyName) {
		QuestionnaireInfoEntity questionnaireInfo = questionnaireInfoService.queryObject(surveyId);
		questionnaireInfo.setName(svyName);
		questionnaireInfo.setStatus("1");
		questionnaireInfoService.update(questionnaireInfo);
		return Result.ok().put("questionnaireInfo", questionnaireInfo);
	}


	/**
	 * 保存题目
	 */
	@RequestMapping("/ajaxSave")
//	@RequiresPermissions("questionnaireinfo:save")
	@ResponseBody
	public Result ajaxSave(@RequestBody QuQuestionEntity questionEntity){
		//保存或更新题目
		if(questionEntity.getId() == null){
			quQuestionService.save(questionEntity);
		}else{
			quQuestionService.update(questionEntity);
		}
		//保存或更新多选题
		List<QuCheckboxEntity> checkboxEntities = questionEntity.getQuCheckboxs();
		if(checkboxEntities != null && checkboxEntities.size() > 0){
			for (QuCheckboxEntity checkboxEntity : checkboxEntities) {
				if(checkboxEntity.getId() == null){
					checkboxEntity.setQuId(questionEntity.getId());
					checkboxService.save(checkboxEntity);
				}else {
					checkboxService.update(checkboxEntity);
				}
			}
		}

		//保存或更新单选题
		List<QuRadioEntity> quRadios = questionEntity.getQuRadios();
		if(quRadios != null && quRadios.size() > 0){
			for (QuRadioEntity quRadio : quRadios) {
				if(quRadio.getId() == null){
					quRadio.setQuId(questionEntity.getId());
					radioService.save(quRadio);
				}else{
					radioService.update(quRadio);
				}
			}
		}
		return Result.ok();
	}

	/**
	 * 删除问卷
	 */
	@RequestMapping("/delete")
	@ResponseBody
//	@RequiresPermissions("questionnaireinfo:delete")
	public Result delete(Model model, String id, HttpServletRequest request){
		questionnaireInfoService.delete(id);

		return Result.ok();
	}

	/**
	 * 获取问卷参数方法
	 */
	public Map<String, Map<String, Object>> buildSaveSurveyMap(HttpServletRequest request) {
		Map<String, Map<String, Object>> quMaps = new HashMap<>();
		Map<String, Object> radioMaps = WebUtils.getParametersStartingWith(request, "qu_RADIO" + "_");
		Map<String, Object> checkboxMaps = WebUtils.getParametersStartingWith(request, "qu_CHECKBOX" + "_");
		//-----
		for (String key : radioMaps.keySet()) {
			String enId = key;
			Integer quItemId = Integer.valueOf(radioMaps.get(key).toString());
			AnRadioEntity anRadio = new AnRadioEntity();
			anRadio.setQuId(Integer.parseInt(enId));
			anRadio.setQuItemId(quItemId);
			radioMaps.put(key, quItemId);
		}
		quMaps.put("radioMaps", radioMaps);
		for (String key : checkboxMaps.keySet()) {
			String dfillValue = checkboxMaps.get(key).toString();
			Map<String, Object> map = WebUtils.getParametersStartingWith(request, dfillValue);
			for (String key2 : map.keySet()) {
				AnCheckboxEntity anCheckbox = new AnCheckboxEntity();
				map.put(key2, anCheckbox);
			}
			checkboxMaps.put(key, map);
		}
		quMaps.put("checkboxMaps", checkboxMaps);
		return quMaps;
	}

}
